<?php
# +--------------------------------------------------------
# |Description: 河北新点电商接口
# +--------------------------------------------------------
# |Company: 北京申合信科技发展有限公司
# +--------------------------------------------------------
# |WebSite: www.shenhexin.com
# +--------------------------------------------------------
# |Author: Akon<18363851918@163.com>
# +--------------------------------------------------------
# |Date: 2017年6月29日
# +--------------------------------------------------------
namespace Ecservice\Controller;
use Think\Controller;
class IndexController extends Controller {

    /**
     * 获取接口令牌
     * 【URL】http://api.shenhexin.com/ecservice/getAccessToken
     */
    public function getAccessToken() {
        $receive = getRequestData('post');      //接受数据

        A('Common')->errorMsg($receive['appKey'], 'appKey不能为空');
        A('Common')->errorMsg($receive['appSecret'], 'appSecret不能为空');
        A('Common')->errorMsg($receive, '参数不合法', 1, 2);

        $map = array('name'=>$receive['appKey'], 'api_pass'=>$receive['appSecret']);
        $info = M('members', 'sdb_', 'DB_CONFIG2')->where($map)
                                                  ->find();
        if (!$info) {
            A('Common')->errorMsg($info,'账号或密码不正确');
        }

        $token= generate_token($receive['appKey'], $receive['appSecret']);
        $update_field = array(
            'access_token' => $token,
            'create_time'  => time(),
            'expire_time'  => time() + 86400
        );
        M('members', 'sdb_', 'DB_CONFIG2')->where($map)
                                          ->setField($update_field);     //将token及过期时间写入sdb_members表

        $res = array(
            'accessToken' => $token,
            'returnMsg'   => 'access_token生成，过期时间为24小时',
            'isSuccess'   => true
        );
        exit(json_encode($res,JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE));
    }

    #========================= 商品模块start =========================#

    /**
     * 获取品类接口
     * 【URL】http://api.shenhexin.com/ecservice/getProductCategory
     */
    public function getProductCategory() {
        //1、接受数据
        $receive = getRequestData('post');
        //2、校验
        A('Common')->checkParam(
            $receive['appKey'],
            $receive['accessToken'],
            $receive,
            2
        );
        //5、所有校验通过，返回数据
        $ori_cates = M('goods_cat', 'sdb_', 'DB_CONFIG2')->field('cat_name name,cat_id categoryId')
                                                         ->select();
        A('Common')->errorMsg($ori_cates, '没有查询到数据，请检查输入是否正确');
        foreach($ori_cates as $key=>$val){
            $cur_cates[$key]['name']       = $val['name'];
            $cur_cates[$key]['categoryId'] = $val['categoryid'];
        }
        $res = array(
            'result' => $cur_cates,
            'returnMsg' => '品目数据返回成功',
            'isSuccess' => true
        );
        exit(json_encode($res, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
    }


    function test(){
        /*$arr = array(
            'appKey' => 'epoint',
            'accessToken' => 'e3a9e14beea8782f2abd922d64516899_1503277869',
            'skus' => array(
                0 => 'G57B581AECDE2B',
                1 => 'G57B659C405BB8'
            ),
            'cityId' => '23'
        );*/
        $arr1 = array(
            'accessToken' => 'e3a9e14beea8782f2abd922d64516899_1503277869',
            'appKey' => 'epoint',
            'tradeNo' => '11748688',
            'sku' => array(
                0 => array(
                    'skuId' => 'G57B581AECDE2B',
                    'num' => '1',
                    'price' => '5.35'
                ),
                1 => array(
                    'skuId' => 'G57B659C405BB8',
                    'num' => '100',
                    'price' => '56.98'
                )
            ),
            'name' => '贾瑞一',
            'provinceId' => '110',
            'cityId' => '025',
            'countyId' => '01',
            'townId' => '01',
            'address' => '莲花池东路',
            'phone' => '025-88888888',
            'mobile' => '18363851918',
            'email' => '1101945992@qq.com',
            'invoiceState' => '1',
            'invoiceType' => '2',
            'companyName' => '北京申合信科技发展有限公司',
            'invoiceContent' => '1',
            'amount' => '5703.35',
            'freight' => '0',
            'payment' => '1',
            'remark' => '备注'
        );
        $arr = array(
            'accessToken' => 'e3a9e14beea8782f2abd922d64516899_1503277869',
            'appKey' => 'epoint',
            'orderId' => '2017082109405040',
            'sku' => array(
                0 => array(
                    'skuId' => 'G57B581AECDE2B',
                    'num' => '1'
                ),
                1 => array(
                    'skuId' => 'G57B659C405BB8',
                    'num' => '50'
                )
            )
        );
        echo http_build_query($arr);
    }
    /**
     * 获取商品池接口
     * 【URL】http://api.shenhexin.com/ecservice/getProductPool
     */
    public function getProductPool() {
        //1、接受数据
        $receive = getRequestData('post');
        //2、校验
        A('Common')->checkParam(
            $receive['appKey'],
            $receive['accessToken'],
            $receive,
            3
        );
        A('Common')->errorMsg($receive['categoryId'], 'categoryId不能为空');
        //3、所有校验通过，返回数据
        $ori_skus = M('goods', 'sdb_', 'DB_CONFIG2')->where(array('cat_id'=>$receive['categoryId']))
                                                    ->select();
        A('Common')->errorMsg($ori_skus, '此分类下没有商品');
        foreach($ori_skus as $key=>$val){
            if(strlen($ori_skus[$key]['intro']) < 20) break;
            $cur_skus[] = $val['bn'];
        }
        $res = array(
            'sku' => $cur_skus,
            'returnMsg' => '商品池数据返回成功',
            'isSuccess' => true
        );
        exit(json_encode($res, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
    }

    /**
     * 获取商品详情接口
     * 【URL】http://api.shenhexin.com/ecservice/getProductDetail
     */
    public function getProductDetail() {
        //1、接受数据
        $receive = getRequestData('post');
        //2、校验
        A('Common')->checkParam(
            $receive['appKey'],
            $receive['accessToken'],
            $receive,
            3
        );
        A('Common')->errorMsg($receive['sku'], 'sku不能为空');
        //5、所有校验通过，返回数据
        $query      = "
                      SELECT a.bn sku,a.weight,a.big_pic image,a.marketable state,a.brand,a.name,a.unit saleUnit,b.cat_name category,a.intro introduction
                      FROM sdb_goods a
                      join sdb_goods_cat b
                      on a.cat_id=b.cat_id
                      where bn='{$receive['sku']}'";
        $ori_detail = M('','','DB_CONFIG2')->query($query);
        A('Common')->errorMsg($ori_detail, '没有查询到数据');
        $res = array(
            'sku' => $ori_detail[0]['sku'],
            'weight' => $ori_detail[0]['weight'],
            'image' => 'http://www.shenhexin.com/' .explode('|',$ori_detail[0]['image'])[0],
            'state' => $ori_detail[0]['state'] ? 1 : 0,
            'brand' => $ori_detail[0]['brand'],
            'model' => '',
            'name' => $ori_detail[0]['name'],
            'productArea' => '',
            'upc' => '',
            'saleUnit' => '',
            'category' => $ori_detail[0]['category'],
            'introduction' => $ori_detail[0]['introduction'],
            'param' => '',
            'returnMsg' => '商品详情数据返回成功',
            'isSuccess' => true
        );
        exit(json_encode($res, JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE));
    }

    /**
     * 获取商品评价接口
     * 【URL】http://api.shenhexin.com/ecservice/getProductReview
     */
    public function getProductReview() {
        //1、接受数据
        $receive = getRequestData('post');
        $skus = jsonToArr($receive['skuId']);
        //2、校验
        A('Common')->checkParam(
            $receive['appKey'],
            $receive['accessToken'],
            $receive,
            3
        );
        A('Common')->errorMsg($receive['skuId'], 'sku不能为空');
        //3、所有校验通过，返回数据
        foreach($skus as $key=>$val){
            $temp[$key]['skuId']        = $val;
            $temp[$key]['averageScore'] = 0;
            $temp[$key]['generalRate']  = 0;
            $temp[$key]['goodRate']     = 0;
            $temp[$key]['poorRate']     = 0;
        }
        $res = array(
            'result'    => $temp,
            'returnMsg' => '数据返回成功',
            'isSuccess' => true
        );
        exit(json_encode($res, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
    }

    /**
     * 获取商品图片接口
     * 【URL】 http://api.shenhexin.com/ecservice/getProductImage
     */
    public function getProductImage() {
        //1、接受数据
        $receive = getRequestData('post');
        $skus = jsonToArr($receive['sku']);
        //2、校验
        A('Common')->checkParam(
            $receive['appKey'],
            $receive['accessToken'],
            $receive,
            3
        );
        A('Common')->errorMsg($receive['sku'], 'sku不能为空');
        //3、校验通过，返回数据
        $sku_str = addApostropheToString(implode(',', $skus));
        $primary_img_sql = "SELECT goods_id,bn,big_pic FROM sdb_goods WHERE bn IN (".$sku_str.")";
        $primary_img = M('', '', 'DB_CONFIG2')->query($primary_img_sql);
        foreach($primary_img as $key=>$val)
        {
            $goods_id[$key] = $val['goods_id'];
        }
        $other_img_sql = "SELECT goods_id,big FROM sdb_gimages WHERE goods_id IN (".implode(',', $goods_id).")";
        $other_img = M('', '', 'DB_CONFIG2')->query($other_img_sql);
        foreach($primary_img as $key=>$val)
        {
            $images[$key]['skuId'] = $val['bn'];
            foreach($other_img as $k=>$v)
            {
                if($v['goods_id'] == $val['goods_id'])
                {
                    $images[$key]['urls'][$k]['path'] = 'http://www.shenhexin.com/'.explode('|', $v['big'])[0];
                    $images[$key]['urls'][$k]['primary'] = 0;
                }
            }
        }
        foreach($primary_img as $key=>$val)
        {
            foreach($images as $k=>$v)
            {
                if($val['bn'] == $v['skuId'])
                {
                    $images[$k]['urls'][100]['path'] = 'http://www.shenhexin.com/'.explode('|', $val['big_pic'])[0];
                    $images[$k]['urls'][100]['primary'] = 1;
                }
            }
        }
        foreach($images as $key=>$val)
        {
            $temp[$key]['skuId'] = $val['skuId'];
            $temp[$key]['urls'] = array_values($val['urls']);
        }
        $res = array(
            'result'    => $temp,
            'returnMsg' => '数据返回成功',
            'isSuccess' => true
        );

        exit(json_encode($res, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
    }

    /**
     * 商品上下架状态查询接口
     * 【URL】 http://api.shenhexin.com/ecservice/getProductOnShelvesInfo
     */
    public function getProductOnShelvesInfo(){
        //1、接受数据
        $receive = getRequestData('post');
        $skus = jsonToArr($receive['sku']);
        //2、校验
        A('Common')->checkParam(
            $receive['appKey'],
            $receive['accessToken'],
            $receive,
            3
        );
        A('Common')->errorMsg($receive['sku'], 'sku不能为空');
        //3、验证通过，返回数据
        $sku_str = addApostropheToString(implode(',', $skus));
        $sql = "SELECT bn,marketable FROM sdb_goods WHERE bn IN (".$sku_str.")";
        $goods_state = M('','','DB_CONFIG2')->query($sql);
        foreach($goods_state as $key=>$val)
        {
            $temp[$key] = array(
                'skuId' => $val['bn'],
                'listState' => $val['marketable'] ? 1 : 0
            );
        }
        A('Common')->errorMsg($goods_state, '请检查输入是否正确');
        $res = array(
            'onShelvesList' => $temp,
            'returnMsg' => '数据返回成功',
            'isSuccess' => true
        );
        exit(json_encode($res, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
    }
    #========================= 商品模块end =========================#

    #========================= 地址模块start =========================#

    /**
     * 查询省信息接口
     * 【URL】 http://api.shenhexin.com/ecservice/getProvinceInfo
     */
    public function getProvinceInfo() {
        //1、接受数据
        $receive = getRequestData('post');
        //2、校验
        A('Common')->checkParam(
            $receive['appKey'],
            $receive['accessToken'],
            $receive,
            3
        );
        A('Common')->errorMsg($receive['v'], '版本号不能为空');
        //3、校验成功，返回数据
        $province = M('regions', 'sdb_', 'DB_CONFIG2')->where(array('region_grade'=>1))
                                                      ->field('region_id code,local_name name')
                                                      ->select();
        A('Common')->errorMsg($province, '请检查输入是否正确');
        $res = array(
            'province'  => $province,
            'returnMsg' => '数据返回成功',
            'isSuccess' => true
        );
        exit(json_encode($res, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
    }

    /**
     * 查询市信息接口
     * 【URL】 http://api.shenhexin.com/ecservice/getCityInfo
     */
    public function getCityInfo() {
        //1、接受数据
        $receive = getRequestData('post');
        //2、校验
        A('Common')->checkParam(
            $receive['appKey'],
            $receive['accessToken'],
            $receive,
            3
        );
        A('Common')->errorMsg($receive['provinceId'], 'provinceId不能为空');
        //3、校验成功，返回数据
        $city = M('regions', 'sdb_', 'DB_CONFIG2')->where(array('p_region_id'=>$receive['provinceId']))
            ->field('region_id code,local_name name')
            ->select();
        A('Common')->errorMsg($city, '请检查输入是否正确');
        $res = array(
            'city'  => $city,
            'returnMsg' => '数据返回成功',
            'isSuccess' => true
        );
        exit(json_encode($res, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
    }

    /**
     * 查询区信息接口
     * 【URL】 http://api.shenhexin.com/ecservice/getDistrictInfo
     */
    public function getDistrictInfo() {
        //1、接受数据
        $receive = getRequestData('post');
        //2、校验
        A('Common')->checkParam(
            $receive['appKey'],
            $receive['accessToken'],
            $receive,
            3
        );
        A('Common')->errorMsg($receive['cityId'], 'cityId不能为空');
        //3、校验成功，返回数据
        $district = M('regions', 'sdb_', 'DB_CONFIG2')->where(array('p_region_id'=>$receive['cityId']))
            ->field('region_id code,local_name name')
            ->select();
        A('Common')->errorMsg($district, '请检查输入是否正确');
        $res = array(
            'city'  => $district,
            'returnMsg' => '数据返回成功',
            'isSuccess' => true
        );
        exit(json_encode($res, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
    }
    #========================= 地址模块end =========================#

    /**
     * 库存查询接口
     * 【URL】 http://api.shenhexin.com/ecservice/getProductInventory
     */
    public function getProductInventory() {
        //1、接受数据
        $receive = getRequestData('post');
        //2、校验
        A('Common')->checkParam(
            $receive['appKey'],
            $receive['accessToken'],
            $receive,
            6
        );
        A('Common')->errorMsg($receive['sku'], 'sku不能为空');
        A('Common')->errorMsg($receive['num'], 'num不能为空');
        //3、校验成功，返回数据
        $info = M('goods', 'sdb_', 'DB_CONFIG2')->where(array('bn'=>$receive['sku']))->field('bn,store')->find();
        $store = array(
            'skuId' => $info['bn'],
            'state' => judgeStore($info['store'], $receive['num']),
            'returnMsg' => '数据返回成功',
            'isSuccess' => true
        );
        exit(json_encode($store, JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE));
    }

    /**
     * 商品库存状态批量查询接口
     * 【URL】 http://api.shenhexin.com/ecservice/bathQueryInventoryStatus
     */
    public function bathQueryInventoryStatus() {
        //1、接受数据
        $receive = getRequestData('post');
        //2、校验
        A('Common')->checkParam(
            $receive['appKey'],
            $receive['accessToken'],
            $receive,
            4
        );
        if(count($receive['skus']) > 50) {
            A('Common')->errorMsg(0, '查询数量超过上限，最多为50条');
        }
        A('Common')->errorMsg($receive['skus'], 'sku不能为空');
        A('Common')->errorMsg($receive['cityId'], 'cityId不能为空');
        //3、校验成功，返回数据
        $skus = addApostropheToString(implode(',', $receive['skus']));
        $info = M('goods', 'sdb_', 'DB_CONFIG2')->where("bn in ($skus)")
                                                ->field('bn,store')
                                                ->select();
        foreach($info as $key=>$val){
            $store[$key]['skuId'] = $val['bn'];
            $store[$key]['state'] = $val['bn']=0 ? '2' : 0;
        }
        $res = array(
            'result'    => $store,
            'returnMsg' => '数据返回成功',
            'isSuccess' => true
        );
        exit(json_encode($res, JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE));
    }
    #========================= 库存模块start =========================#

    #========================= 价格模块start =========================#
    /**
     *获取商品折扣价格接口
     * 【URL】http://api.shenhexin.com/ecservice/queryCountPrice
     */
    public function queryCountPrice() {
        //1、接受数据
        $receive = getRequestData('post');
        //2、校验
        A('Common')->checkParam(
            $receive['appKey'],
            $receive['accessToken'],
            $receive,
            4
        );
        A('Common')->errorMsg($receive['sku'], 'sku不能为空');
        A('Common')->errorMsg($receive['cityId'], 'cityId不能为空');
        //3、校验成功，返回数据
        $skus = addApostropheToString(implode(',', $receive['sku']));
        $info = M('goods', 'sdb_', 'DB_CONFIG2')->where("bn in ($skus)")->field('bn,price')->select();
        foreach($info as $key=>$val)
        {
            $temp[$key] = array(
                'skuId' => $val['bn'],
                'price' => $val['price'],
                'discount' => 0.93
            );
        }
        $res = array(
            'priceList' => $temp,
            'cityId'    => $receive['cityId'],
            'returnMsg' => '数据返回成功',
            'isSuccess' => true
        );
        exit(json_encode($res, JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE));
    }
    #========================= 价格模块end =========================#

    #========================= 订单模块start =========================#

    /**
     * 创建订单接口
     * 【URL】http://api.shenhexin.com/ecservice/createOrder
     */
    public function createOrder() {
        $receive = getRequestData('post');
        checkReceive($receive);
        A('Common')->errorMsg($receive['appKey'], 'appKey不能为空');
        A('Common')->errorMsg($receive['accessToken'], 'accessToken不能为空');
        A('Common')->checkAccessToken($receive['accessToken'], $receive['appKey']);

        foreach($receive['sku'] as $key=>$val){
            $skuArr[$key] = $val['skuId'];           //处理sku
        }
        $skuStr = addApostropheToString(implode(',', $skuArr));
        $temp_1 = M('goods', 'sdb_', 'DB_CONFIG2')->where("bn in ($skuStr)")->field('name')->select();

        foreach($temp_1 as $key=>$val){
            $goods_name[$key] = $val['name'] . '('.$receive['sku'][$key]['num'].')';       //处理商品名称
        }


        $temp_2 = array($receive['provinceId'], $receive['cityId'], $receive['countyId']);
        $ship_area = 'mainland:';
        foreach($temp_2 as $key=>$val){
            $area = M('regions', 'sdb_', 'DB_CONFIG2')->where(array('region_id'=>$val))->field('local_name')->find();
            $ship_area .= '/' .$area['local_name'];
        }

        $members = M('members', 'sdb_', 'DB_CONFIG2')->where(array('name'=>$receive['appKey']))->field('member_id')->find();
        $order_id = date('YmdHis') . mt_rand(0,9) . mt_rand(0,9);
        $is_exists_order_id = M('orders','sdb_','DB_CONFIG2')->where(array('order_id'=>$order_id))->count();
        if($is_exists_order_id){
            $order_id = date('YmdHis') . mt_rand(0,9) . mt_rand(0,9);
        }
        $orders = array(
            'order_id'     => $order_id,
            'status'      => 'active',
            'tostr'       => implode(',', $goods_name),
            'ship_name'   => $receive['name'],
            'ship_area'   => $ship_area . ':' . $receive['countyId'],
            'ship_mobile' => $receive['mobile'],
            'ship_email'  => $receive['email'],
            'is_tax'      => boolval($receive['invoiceState']),
            'tax_type'    => $receive['invoiceType'] == 1 ? '增票' : '普票',
            'tax_company' => $receive['companyName'],
            'tax_content' => $receive['invoiceContent'],
            'cost_item'   => $receive['amount'],
            'shipping'    => '',
            'member_id'   => $members['member_id'],
            'is_confirm'  => 1,
            'gc_order_no' => $receive['tradeNo'],
            'createtime'  => time(),
            'order_source'=> 1           //订单来源，0 商城订单， 1 第三方订单
        );
        foreach($receive['sku'] as $key=>$val){
            $order_items[$key]['order_id'] = $orders['order_id'];
            $order_items[$key]['name']    = $temp_1[$key]['name'];
            $order_items[$key]['price']   = $val['price'];
            $order_items[$key]['amount']  = $val['price'] * $val['num'];
            $order_items[$key]['bn']      = $val['skuId'];
            $order_items[$key]['nums']    = $val['num'];
            $product_id = M('products', 'sdb_', 'DB_CONFIG2')->where(array('bn'=>$val['skuId']))->field('product_id')->find();
            $order_items[$key]['product_id'] = $product_id['product_id'] ? $product_id['product_id'] : 0;
        }
        $order_res = M('orders', 'sdb_', 'DB_CONFIG2')->add($orders);                       //订单主表写入
        foreach($order_items as $key=>$val){
            $order_items_res = M('order_items', 'sdb_', 'DB_CONFIG2')->add($val);           //订单副表写入
            $res['orderId']                 = $val['order_id'];
            $res['sku'][$key]['skuId']      = $val['bn'];
            $res['sku'][$key]['num']        = $val['nums'];
            $res['sku'][$key]['arriveData'] = date('Y-m-d H:i:s',time() + 24*3*60*60);
            $res['amount']                  = $receive['amount'];
            $res['freight']                 = 0;
            $res['errorCode']               = '';
            $res['returnMsg']               = '数据返回成功';
            $res['isSuccess']               = true;
        }
        exit(json_encode($res, JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE));
    }

    /**
     * 订单退货接口
     * 【URL】 http://api.shenhexin.com/ecservice/cancelOrder
     */
    public function cancelOrder() {
        //1、接受数据
        $receive = getRequestData('post');
        //2、校验
        A('Common')->checkParam(
            $receive['appKey'],
            $receive['accessToken'],
            $receive,
            4
        );
        A('Common')->errorMsg($receive['sku'], 'sku不能为空');
        A('Common')->errorMsg($receive['orderId'], 'orderId不能为空');
        //3、校验通过，返回数据
        foreach($receive['sku'] as $key=>$val){
            $str = '0-' . $val['num'];
            M('order_items', 'sdb_', 'DB_CONFIG2')->where(array('order_id'=>$receive['orderId'],'bn'=>$val['skuId']))->setField('status',$str);

            $res['orderId']                        = $receive['orderId'];
            $res['infoList'][$key]['partNumber']   = $val['skuId'];
            $res['infoList'][$key]['status']       = 1;
            $res['infoList'][$key]['unableReason'] = '';
            $res['returnMsg'] = '数据返回成功';
            $res['isSuccess'] = 'true';
        }
        exit(json_encode($res, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES));
    }

    /**
     * 获取订单物流状态接口
     * 【URL】 http://api.shenhexin.com/ecservice/getOrderLogisticsStatus
     */
    public function getOrderLogisticsStatus() {
        //1、接受数据
        $receive = getRequestData('post');
        //2、校验
        A('Common')->checkParam(
            $receive['appKey'],
            $receive['accessToken'],
            $receive,
            4
        );
        A('Common')->errorMsg($receive['skuId'], 'sku不能为空');
        A('Common')->errorMsg($receive['orderId'], 'orderId不能为空');
        //3、校验通过，返回数据
        $info = M('orders', 'sdb_', 'DB_CONFIG2')->where(array('order_id'=>$receive['orderId']))->field('ship_status,createtime')->find();
        if($info['ship_status'] == 0) {
            $ship_status = '未发货';
        } else if($info['ship_status'] == 1) {
            $ship_status = '已发货';
        }
        $res = array(
            'orderId' => $receive['orderId'],
            'sku'     => $receive['skuId'],
            'orderLogisticStatus' => array(
                'operateState' => $ship_status,
                'operatorTime' => $info['createtime']
            ),
            'shippingTime' => date('Y-m-d',time()+ 86400),
            'receiveTime'  => date('Y-m-d',time()+ 345600),
            'returnMsg'    => '数据返回成功',
            'isSuccess'    => 'true'
        );
        exit(json_encode($res, JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE));
    }

    /**
     * 获取订单状态接口
     * 【URL】 http://api.shenhexin.com/ecservice/getOrderStatus
     */
    public function getOrderStatus() {
        //1、接受数据
        $receive = getRequestData('post');
        //2、校验
        A('Common')->checkParam(
            $receive['appKey'],
            $receive['accessToken'],
            $receive,
            3
        );
        A('Common')->errorMsg($receive['orderId'], 'orderId不能为空');
        //3、校验通过，返回数据
        $status = M('orders', 'sdb_', 'DB_CONFIG2')->where(array('order_id'=>$receive['orderId']))->field('pay_status')->find();
        $items = M('order_items', 'sdb_','DB_CONFIG2')->where(array('order_id'=>$receive['orderId']))->field('bn')->select();
        foreach($items as $key=>$val){
            $temp[$key]['skuId'] = $val['bn'];
            $temp[$key]['statusName'] = changeOrderStatus($status['pay_status']);
        }
        $res = array(
            'orderId' => $receive['orderId'],
            'orderStatus' =>changeOrderStatus($status['pay_status']),
            'sku'     => $temp,
            'returnMsg' => '数据返回成功',
            'isSuccess' => true
        );
        exit(json_encode($res, JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE));
    }
    #========================= 订单模块end =========================#

    #========================= 品牌授权start =========================#
    public function getBrandAuthorization(){
        //1、接受数据
        $receive = getRequestData('post');
        //2、校验
        A('Common')->checkParam(
            $receive['appKey'],
            $receive['accessToken'],
            $receive,
            4
        );
        A('Common')->errorMsg($receive['categoryId'], 'categoryId不能为空');
        A('Common')->errorMsg($receive['brandName'], 'brandName不能为空');

        $brand = M('auth_brand', 'sdb_', 'DB_CONFIG2')->where(array('brand_name'=>$receive['brandName']))->find();
        if($brand){
            $res = array(
                'categoryId' => $receive['categoryId'],
                'brandName'  => $brand['brand_name'],
                'orderStatus'=> 1,
                'startDate'  => date('Y-m-d', $brand['start_time']),
                'endDate'    => date('Y-m-d', $brand['end_time']),
                'factoryName' => $brand['factory_name'],
                'authorizationUrl' => 'http://manager.shenhexin.com' . $brand['auth_url'],
                'returnMsg'  => '数据返回成功',
                'isSuccess'  => 'true'
            );
        }else{
            $res = array(
                'categoryId'  => $receive['categoryId'],
                'brandName'   => $receive['brandName'],
                'orderStatus' => 0,
                'startDate'   => '0000-00-00',
                'endData'     => '0000-00-00',
                'factoryName' => '',
                'authorizationUrl' => '',
                'returnMsg' => '数据返回成功',
                'isSuccess' => 'true'
            );
        }
        exit(json_encode($res, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES));
    }
    #========================= 品牌授权end =========================#
}